import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import path from 'path'
import process from 'process'

// https://vitejs.dev/config/
export default ({ mode }) => {
  // 参数mode为开发模式或生产模式
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV, VITE_BASE_URL } = env
  return defineConfig({
    plugins: [uni()],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    // 跨域代理
    server: {
      proxy: {
        '/api': {
          // 代理地址
          target: VITE_APP_ENV === 'development' ? 'http://localhost:8080' : 'http://www.zhangshiyang.com:8080',
          changeOrigin: true, // 是否允许跨域
          secure: true,
          rewrite: (path) => path.replace(/^\/api/, '') // api标志替换为''
        }
      }
    },
    // 项目打包配置
    base: VITE_BASE_URL,
    build: {
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: false,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    }
  })
}
